package com.auto.util;

import java.util.List;
import java.util.Set;

import com.auto.entity.IdEntity;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

public class BeanUtil {
	public static <T extends IdEntity> T getBeanById(Long id,
			Class<T> requiredType) {
		T bean = null;
		try {
			bean = requiredType.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		bean.setId(id);
		return bean;
	}

	public static <T extends IdEntity> Set<T> getBeanByIds(Long[] ids,
			Class<T> requiredType) {
		Set<T> beans = Sets.newHashSet();
		for (Long id : ids) {
			beans.add(getBeanById(id, requiredType));
		}
		return beans;
	}
	public static <T extends IdEntity> List<T> getBeanByIdsList(Long[] ids,
			Class<T> requiredType) {
		List<T> beans = Lists.newArrayList();
		for (Long id : ids) {
			beans.add(getBeanById(id, requiredType));
		}
		return beans;
	}
	
	public static <T, S> T convert(S bean, Class<T> toType){
		ObjectMapper m = new ObjectMapper();
		T props = m.convertValue(bean, toType);
		return props;
	}
}
